<?php
/**
 * @author Etienne AUDOUIN <etienne.audouin@ingesup.com>
 * @author Adrien MARIANELLI <adrien.marianelli@ingesup.com>
 * @version 1.0
 * @package models
 * @subpackage mailing list
 */
class Mailinglist_model extends CI_Model {
    /**
     * Reference a la table mailinglist
     * @var string
     */
    protected $table="mailinglist";

    /**
     * Cree une nouvelle liste de diffusion
     * {@source}
     * @param string $name
     * @param string $id_contacts
     * @return bool
     */
    public function create ($name, $id_contacts) {
        $this->db->set('name', $name);
        $ids="";
        foreach ($id_contacts as $id){
            $ids = $ids.":".$id;
        }
        $ids = substr($ids, 1);
        $this->db->set('id_contacts', $ids);
        return $this->db->insert($this->table);
    }

    /**
     * Renvoie une liste de diffusion
     * {@source}
     * @todo Ne renvoie q'un resultat, placer array_pop
     * @param int $id
     * @return array
     */
    public function read ($id) {
        return $this->db->select('*')
                ->from($this->table)
                ->where('id', (int) $id)
                ->get()
                ->result();
    }

    /**
     * Renvoie toute les listes de difusion
     * {@source}
     * @return array
     */
    public function readAll () {
        return $this->db->select('*')
                ->from($this->table)
                ->get()
                ->result();
    }

    /**
     * Met a joue une liste de diffusion
     * {@source}
     * @param int $id
     * @param string $id_contacts
     * @param string $name
     * @return array
     */
    public function update ($id, $id_contacts, $name = null) {
        if ($name == null || $id == null)
            return false;
        
        $this->db->set('name', $name);
        
        if ($id_contacts != null)
            $this->db->set('id_contacts', $id_contacts);
        
        $this->db->where('id', (int) $id);
        
        return $this->db->update($this->table);
        
    }

    /**
     * Supprime une liste de diffusion
     * {@source}
     * @param int $id
     * @return bool
     */
    public function delete ($id) {
        return $this->db->where('id', (int) $id)->delete($this->table);
    }

    /**
     * Renvoie toute les listes de diffusion auxquelle appartient un contact
     * {@source}
     * @param int $id
     * @return array
     */
    public function trace ($id){
        $begin =  $this->db->query("
        select *
        from mailinglist
        where id_contacts like '".$id.":%'")->result();

        $inside = $this->db->query("
        select *
        from mailinglist
        where id_contacts like '%:".$id.":%'")->result();

        $end = $this->db->query("
        select *
        from mailinglist
        where id_contacts like '%:".$id."'")->result();

        $alone = $this->db->query("
        select *
        from mailinglist
        where id_contacts like '".$id."'")->result();

        $result =array();
        foreach ($begin as $list)   array_push ($result, $list);
        foreach ($inside as $list)  array_push ($result, $list);
        foreach ($end as $list)      array_push ($result, $list);
        foreach ($alone as $list)   array_push ($result, $list);
        return $result;
    }
}
?>
